var app = getApp()
var maxPageIndex
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    buthidden: false,
    wrapCategory: false,
    orderStatus: [
      {
        statusName: '全部',
        statusCode: 0
      },
      {
        statusName: '未结款',
        statusCode: 1
      },
      {
        statusName: '已结款',
        statusCode: 2
      } //0全部 1未结款 2已结款
    ],
    statusSelect: 0,
    listDate: [
      {
        name: '全部',
        id: 1
      },
      {
        name: '近7天',
        id: 2
      },
      {
        name: '近一个月',
        id: 3
      },
      {
        name: '近三个月',
        id: 4
      },
      {
        name: '三个月前',
        id: 5
      }
    ], //1:全部 2:近7天 3:近1个月 4:近3个月 5:3个月前
    dateSelect: 1,
    pageIndex: 1,
    result: '',
    placeStatus: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},
  onShow: function() {
    var that = this
    wx.pageScrollTo({ scrollTop: 0, duration: 0 })
    that.setData({ pageIndex: 1 })
    that.onGetOutSite()
  },
  //销货对账单列表信息
  onGetOutSite: function() {
    var that = this
    var arr = new Array()
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    })
    wx.request({
      method: 'POST',
      url: app.preUrl + 'balanceBill',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        settleStatus: that.data.statusSelect, //0全部 1未结款 2已结款
        timeSection: that.data.dateSelect, //时间区间 1:全部 2:近7天 3:近1个月 4:近3个月 5:3个月前
        userId: app.globalData.userId,
        page: that.data.pageIndex, //这里初始值为1
        size: 8,
        placeStatus: that.data.placeStatus //销货 状态    2 配送  3 自提
      },
      success: function(res) {
        console.log(res.data.result)
        wx.hideLoading()
        if (res.data.code == 100) {
          maxPageIndex = res.data.total / 8
          arr = res.data.result
          if (that.data.pageIndex != 1) {
            var array = that.data.result
            array = array.concat(arr)
            that.setData({ result: array })
          } else {
            that.setData({ result: arr })
          }
          that.setData({ ifShownoData: true })
        } else if (res.data.code == 400) {
          that.setData({ ifShownoData: false, result: '' })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          })
        }
      },
      fail: function(error) {}
    })
  },
  onSettlement: function(e) {
    var that = this
    wx.showModal({
      // title: '提示',
      content: '确认将该订单状态改为已结款？',
      cancelText: '再等等',
      confirmColor: '#50BEC5',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading({ title: '提交中' })
          var orderSerial = e.currentTarget.dataset.orderserial //订单流水号
          that.setData({ buthidden: true })
          console.log(orderSerial)
          wx.request({
            method: 'POST',
            url: app.preUrl + 'updateSettleStatus',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              orderSerial: orderSerial,
              settleStatus: 2
            },
            success: function(res) {
              wx.hideLoading()
              console.log(res)

              if (res.data.code == 100) {
                that.setData({ buthidden: false, pageIndex: 1 })
                wx.showToast({
                  title: '修改成功',
                  icon: 'success',
                  duration: 800
                })
                setTimeout(function() {
                  that.onGetOutSite()
                }, 1000)
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.message,
                  confirmColor: '#56BDE0',
                  showCancel: false
                })
                that.setData({ buthidden: false })
              }
            },
            fail: function(error) {
              that.setData({ buthidden: false })
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },

  //一级菜单选择
  orderStatusList: function(e) {
    var that = this
    that.setData({
      //把选中值放入判断值
      statusSelect: e.currentTarget.dataset.select,
      pageIndex: 1
    })
    that.onGetOutSite()
  },
  //月份选择
  dateList: function(e) {
    var that = this
    that.setData({
      //把选中值放入判断值
      dateSelect: e.currentTarget.dataset.select,
      pageIndex: 1
    })
    that.onGetOutSite()
    that.hideModal()
  },
  showModal: function() {
    var that = this
    that.showModal()
  },

  //显示弹窗事件
  showModal: function(e) {
    var that = this
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 300,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(-200).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      //wrapCategory: true,
      wrapCategory: true
    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function() {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export()
      })
    }, 0)
  },
  //隐藏弹窗事件
  hideModal: function(e) {
    var that = this
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(-200).step()
    that.setData({
      animationData: animation.export()
    })
    setTimeout(function() {
      animation.translateY(0).step()
      that.setData({
        wrapCategory: false,
        animationData: animation.export()
      })
    }, 100)
  },
  toPicture(e) {
    let picture = e.currentTarget.dataset.picture
    wx.navigateTo({
      url: 'account_picture?picture=' + picture
    })
  },
  onPullDownRefresh: function() {
    var that = this
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      result: ''
    })
    that.onGetOutSite()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (maxPageIndex <= that.data.pageIndex) {
      return
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    })

    // 上拉获取更多数据
    that.onGetOutSite()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
